package com.meirengu.webview.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.HttpUtil;
import com.meirengu.utils.MD5Util;
import com.meirengu.webview.common.Constants;
import com.meirengu.webview.dao.KwHnbPushDao;
import com.meirengu.webview.model.KwHnbPush;
import com.meirengu.webview.model.KwHnbPushLog;
import com.meirengu.webview.service.KwHnbPushLogService;
import com.meirengu.webview.service.KwHnbPushService;
import com.meirengu.webview.utils.ConfigUtil;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * KwHnbPush服务实现层
 *
 * @author Mao
 * @create Wed May 23 18:16:39 CST 2018
 */
@Service
public class KwHnbPushServiceImpl extends BaseServiceImpl<KwHnbPush> implements KwHnbPushService {


    Logger logger = LoggerFactory.getLogger(KwHnbPushServiceImpl.class);

    @Autowired
    private KwHnbPushDao kwHnbPushDao;

    @Autowired
    private KwHnbPushLogService kwHnbPushLogService;

    /**
     * 佳佳融
     */
    public Integer applyJjr(String name, Integer age, String birthdayStr, Integer sexVal,
                            String mobile, String ip, String city, Integer loanAmount, Integer houseVal,
                            Integer carVal, Integer policyVal, Integer fundVal, Integer socialSecurityVal,
                            Integer weiVal, Integer creditVal,
                            Integer workTime, Integer salaryVal) {

        StringBuffer sb = new StringBuffer(ConfigUtil.getConfig("jjr.loan.url"));
        String key = ConfigUtil.getConfig("jjr.loan.key");
        String channel = ConfigUtil.getConfig("jjr.loan.channel");
        String src = ConfigUtil.getConfig("jjr.loan.src");
        sb.append("?cid=").append(channel).append("&v=").append(MD5Util.MD5(mobile + 19 + key, "UTF-8")).append("&src=").append(src);
        Map<String, String> params = new HashMap<>();
        params.put("name", name);
        params.put("age", age + "");
        params.put("birthday", birthdayStr);
        params.put("sex", sexVal + "");
        params.put("mobile", mobile);
        params.put("ip", ip);
        params.put("city", city);
        params.put("loan_amount", loanAmount + "");
        params.put("houses", houseVal + "");
        params.put("car", carVal + "");
        params.put("life_policy", policyVal + "");
        params.put("epf_time", fundVal + "");
        params.put("social_security", socialSecurityVal + "");
        params.put("particle_loan", weiVal + "");
        params.put("credit_card", creditVal + "");
        params.put("work_time", workTime + "");
        params.put("income_range", salaryVal + "");
        params.put("reg_time", new Date().getTime() + "");


        //接口调用信息表
        KwHnbPush kwHnbPush = new KwHnbPush();
        kwHnbPush.setInterfaceCode("jjr");
        kwHnbPush.setInterfaceName("佳佳融");
        kwHnbPush.setMobile(mobile);
        kwHnbPush.setCode(Constants.BUSINESS_STATUS_FAIL);


        KwHnbPushLog kwHnbPushLog = new KwHnbPushLog();
        kwHnbPushLog.setInterfaceCode("jjr");
        kwHnbPushLog.setInterfaceName("佳佳融");
        kwHnbPushLog.setMobile(mobile);
        kwHnbPushLog.setSendUrl(sb.toString());
        kwHnbPushLog.setParams(params.toString());

        HttpUtil.HttpResult hr = null;
        try {
            hr = HttpUtil.doPostForm(sb.toString(), params);
            int code = hr.getStatusCode();
            if (code == 200) {
                kwHnbPushLog.setBackUrl(hr.toString());
                kwHnbPushLog.setHttpCode(200);
                kwHnbPushLog.setHttpContent(hr.toString());

                logger.info("==KwHnbPushServiceImpl.applyLoan request success. result:{}", hr.toString());
                String content = hr.getContent();
                JSONObject returnMsg = JSONObject.parseObject(content);
                String rCode = returnMsg.getString("errno");
                String rMsg = returnMsg.getString("msg");
                if ("0".equals(rCode)) {
                    kwHnbPush.setCode(Constants.BUSINESS_STATUS_SUCCESS);
                    kwHnbPush.setMessage("成功");
                    logger.info("==KwHnbPushServiceImpl.applyLoan business success");
                } else {
                    kwHnbPush.setCode(Constants.BUSINESS_STATUS_OTHER);
                    kwHnbPush.setMessage("rCode: " + rCode + " rMsg:" + rMsg);
                    logger.warn("==KwHnbPushServiceImpl.applyLoan business fail");
                }
            } else {
                kwHnbPushLog.setBackUrl(hr.toString());
                kwHnbPushLog.setHttpCode(code);
                kwHnbPushLog.setHttpContent(hr.toString());
                logger.warn("==KwHnbPushServiceImpl.applyLoan request fail. code:{}, result:{}", code, hr.toString());
            }
        } catch (Exception e) {
            kwHnbPushLog.setHttpCode(408);
            kwHnbPushLog.setHttpContent("Exception");
            e.printStackTrace();
            logger.error(ExceptionUtils.getFullStackTrace(e));
        } finally {
            insert(kwHnbPush);
            kwHnbPushLog.setPushId(kwHnbPush.getId());
            kwHnbPushLogService.insert(kwHnbPushLog);
            return kwHnbPush.getCode();
        }
    }


    public int updateSuccess(String id) {
        return kwHnbPushDao.updateSuccess(id);
    }

}
